home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11207 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: coranto.ucs.mun.ca!usenet
  2. From: saustin@terra.nlnet.nf.ca (Steve Austin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: References & Pointers in Borland C++
  5. Date: Wed, 13 Mar 1996 02:04:15 GMT
  6. Organization: Kickham Productions
  7. Message-ID: <4i5aem$9e7@coranto.ucs.mun.ca>
  8. References: <4i26j5$roe@dfw-ixnews3.ix.netcom.com>
  9. NNTP-Posting-Host: n104h130.nlnet.nf.ca
  10. X-Newsreader: Forte Agent .99b.112
  11.  
  12. bob_m@ix.netcom.com(Bob M ) wrote:
  13.  
  14. >I am trying to understand the concept of "References".
  15.  
  16. Me too.
  17.  
  18. >1.
  19. >The Programmer's manuals for Borland Turbo C++ version 1.01 (1990)
  20. >and Borland C++ version 4.02 describe in nearly identical text, that
  21. >it is acceptable to initialize a Reference with a specific number:
  22. >int& ir = 6;
  23. >Both my 3.1 and 4.02 compilers reject this with an error: "Reference 
  24. >initialized with 'int' needs lvalue of type 'int'".
  25. >Which is right, the compiler or the book?
  26.  
  27. I tried this with BCC 4.02 and just got the warning:
  28. "Temporary used to initialize "ir" in function..."
  29.  
  30. This just means that the reference "ir" is bound to an anonymous
  31. temporary, which is not otherwise used.  I couldn't reproduce your
  32. error message.
  33.  
  34. >2.
  35. >Using the Inspector window in the Debugger, I could see that a 
  36. >Reference appears identical to a pointer, except that the compiler
  37. >insists that you initialize it to some selected variable.
  38. >It looks as though one should be able to assign a "filled in"
  39. >pointer to a reference, as follows:
  40. >int num, nother;
  41. >int &ref1 = num, &ref2 = nother;
  42. >int *ptr1;
  43. >
  44. >ptr1 = &ref1;  // sets the pointer to the same address as "num".
  45. >&ref2 = ptr1;  // Change &ref2 to point at "num".
  46. >
  47. >The first assignment is apparently legal, but the second depends
  48. >on the version of the compiler. 3.1 accepts it, while 4.01 says:
  49. >"Lvalue required in function main".
  50. >Should this be legal?
  51.  
  52. As far as I can see, the second assignment tries to set the address of
  53. the object bound to ref2 (i.e. in this case, the address of num) to a
  54. pointer to an integer, which is a meaningless operation since &ref2 is
  55. not a modifiable lvalue and therefore can't appear on the left side of
  56. an expression. 
  57.  
  58. A reference has to be initialized, and then is bound to the same
  59. object for the duration of it's scope. You can't bind "ref2" to "num",
  60. after you've initialized it to "nother".
  61.  
  62. I think the confusion partly arises from the use of "&" to mean two
  63. different but related things, just like with "*"; and from the habit
  64. of writing "int &" instead of "int&", which is clearer.
  65.  
  66. For example "int *px" at least has the merit that "*px" by itself is
  67. an int ( this is the justification given by K&R for writing it this
  68. way), but given "int &ir = i", by itself "&ir" is *not* an int.
  69.  
  70. Better I think to avoid this stylistic quirk and write "int* px"
  71. meaning "px" is a pointer to an integer, and "int& ir = i", meaning
  72. "ir" is a reference to an integer.
  73.  
  74. Hope this helps
  75.  
  76. Steve    
  77.  
  78.  
  79.